#include <vector>
#include <queue>

using namespace std;

class KthLargest {
public:
    int K = 0, size = 0;
    priority_queue<int, vector<int>, greater<int>> pq;
    KthLargest(int k, vector<int>& nums) {
        K = k;
        for (auto& it : nums) pq.emplace(it);
        while (pq.size() > K) {
            pq.pop();
        }
    }

    int add(int val) {
        pq.emplace(val);
        while (pq.size() > K) {
            pq.pop();
        }
        return pq.top();
    }
};
